//
// Created by Suny on 15/3/30.
// Copyright (c) 2015 Suny. All rights reserved.
//

#import "TableViewController.h"
#import "TableViewCellDisplayInfo.h"
#import "TableViewCell.h"
#import "TableViewCellContentView.h"
#import "TabViewController.h"

#define kTableViewCellContentViewRightSpace 0


@interface TableViewController()

@end

@implementation TableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view addSubview:self.tableView];
	self.tableView.editing = self.editing;

}

- (void)setEditing:(BOOL)editing
{
	_editing = editing;

    if (self.isViewLoaded)
    {
        self.tableView.editing = editing;
    }

}

- (NSMutableArray *)modelArray
{
    if (!_modelArray)
    {
        _modelArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _modelArray;
}
#pragma mark -


- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,
                                                                   self.navigationBarHeight,
                                                                   self.view.width,
                                                                   self.view.height - self.navigationBarHeight - self.customTabViewController.tabBarHeight)
                                                  style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate   = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
		
		_tableView.editing = self.isEditing;

    }
    return _tableView;
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (TableViewCellDisplayInfo *)tableView:(UITableView *)tableView cellDisplayInfoAtIndexPath:(NSIndexPath *)indexPath
{
	return [self.modelArray safeObjectAtIndex:indexPath.row];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	TableViewCellDisplayInfo *displayInfo = [self tableView:tableView 
								 cellDisplayInfoAtIndexPath:indexPath];

	if ([displayInfo isKindOfClass:[UIView class]])
	{
		return [(UIView *)displayInfo frame].size .height +2*[(UIView *)displayInfo frame].origin.y;
	}
	else if ([displayInfo isKindOfClass:[TableViewCellDisplayInfo class]])
	{
		return [displayInfo heightForCell];
	}
	else
	{
		return 44;
	}
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCellDisplayInfo *displayInfo = [self tableView:tableView cellDisplayInfoAtIndexPath:indexPath];
	NSString *cellID = @"TableViewCellContentView";

    if (CHECK_VALID_STRING(displayInfo.cellContentClass))
    {
        cellID = displayInfo.cellContentClass;
    }
    else
    {
         NSString *displayInfoClassName = NSStringFromClass([displayInfo class]);
         NSString *cellContentViewName = [displayInfoClassName stringByReplacingOccurrencesOfString:@"DisplayInfo"
                                                                                         withString:@"ContentView"];


        if (NSClassFromString(cellContentViewName))
        {
            cellID = cellContentViewName;
        }

    }
	TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];


    if(cell == nil)
    {
        cell= [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                    reuseIdentifier:cellID];
		
		Class aClass = NSClassFromString(cellID);
		TableViewCellContentView *contentView = nil;
		if (aClass)
		{
			contentView = (TableViewCellContentView *) [[aClass alloc] init];
		}
		else
		{
			contentView = [[TableViewCellContentView alloc] init];
		}
		contentView.tag = 1000;
		
		contentView.frame = CGRectMake(0, 0, tableView.width - kTableViewCellContentViewRightSpace, [displayInfo heightForCell]);
		
		[cell.contentView addSubview:contentView];

	}
	
	cell.indexPath = indexPath;

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.accessoryType = self.accessoryType;

	TableViewCellContentView *contentView = (TableViewCellContentView *) [cell.contentView viewWithTag:1000];
	contentView.userInteractionEnabled = YES;
	CGFloat width = tableView.width;
	width -= kTableViewCellContentViewRightSpace;
	contentView.frame = CGRectMake(0, 0,width , [displayInfo heightForCell]);
	[contentView setNeedsLayout];
	[contentView updateDisplayInfo:displayInfo];

	return cell;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}

- (void)doneLoadingTableViewData:(UITableView *)tableView
{

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	TableViewCellDisplayInfo *displayInfo = [self tableView:tableView cellDisplayInfoAtIndexPath:indexPath];
	[self tableView:tableView didSelectRowAtIndexPath:indexPath withModelInfo:displayInfo.modelInfo];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath withModelInfo:(NSObject *)modelInfo
{

}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.cellEditingStyle;
}

- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
 forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == self.cellEditingStyle && self.selectedBlock)
    {
        self.selectedBlock(self,indexPath);

    }
}

- (void)dealloc
{
	
}

- (void)reloadTableView
{
	[self.tableView reloadData];
}

@end